package com.example.controller;

import com.example.domain.dto.CourseDTO;
import com.example.domain.po.Course;
import com.example.domain.vo.CourseSum;
import com.example.domain.vo.CourseVo;
import com.example.service.CourseService;
import com.example.util.PageBean;
import com.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result<PageBean<CourseVo>> list(
            Integer pageNum,
            Integer pageSize,
            String name,
            String department
    ) {

        return Result.success(courseService.list(pageNum, pageSize, name, department));
    }

    /**
     * 根据播放量(热度)排序获取
     *
     * @return
     */
    @GetMapping("/listByHeat")
    public Result<List<Map<String, Object>>> listByHeat() {
        return Result.success(courseService.listByHeat());
    }


    @PostMapping("/add")
    public Result<String> add(CourseDTO courseDTO) throws IOException {
        courseService.add(courseDTO);
        return Result.success("添加成功");
    }


    @DeleteMapping("/{id}")
    public Result<String> del(@PathVariable("id") Integer id) {
        courseService.del(id);
        return Result.success("删除成功");
    }

    @GetMapping("/listByVip")
    public Result<List<Map<String, Object>>> listByVip(Integer type) {
        System.out.println("type值为"+type);
        return Result.success(courseService.listByVip(type));
    }

    @PutMapping
    public Result<String> update(@RequestBody CourseDTO courseDTO) {
        System.out.println(courseDTO);
        courseService.update(courseDTO);
        return Result.success("修改成功");
    }

    @GetMapping("/grounding")
    public Result<String> grounding(Integer id) {
        courseService.grounding(id);
        return Result.success("上架成功");
    }

    @GetMapping("/off")
    public Result<String> off(Integer id) {
        courseService.off(id);
        return Result.success("下架成功");
    }

    /**
     * 根据症状、疾病、医院、科室、医生的姓名以及名字模糊获取课程集合
     *
     * @param name
     * @return
     */
    @GetMapping("/listByLike")
    public Result<List<Map<String, Object>>> listByLike(String name) {
        if (name == null)
            name = "";
        return Result.success(courseService.listByLike(name));
    }

    /**
     * 根据type查询课程 ,按购买量排序
     */
    @GetMapping("/recommend")
    public Result<PageBean<CourseVo>> recommend(Integer pageNum, Integer pageSize, Integer type,Integer userId) {
        return Result.success(courseService.recommend(pageNum, pageSize, type,userId));
    }
    /**
     * 根据id查询
     * @return
     */
    @GetMapping("/getById")
    public Result<Map<String,Object>> geById(Integer id){
        return Result.success(courseService.getById(id));
    }


    @GetMapping("/findById")
    public Result<Course> findById(Integer id) {
        return Result.success(courseService.findById(id));
    }


    @GetMapping("sum")
    public Result<CourseSum> sum(Integer userId) {
        return Result.success(courseService.sum(userId));
    }

    @GetMapping("sumBuy")
    public Result<CourseSum> sumBuy(Integer userId) {
        return Result.success(courseService.sumBuy(userId));
    }

    //根据章节id获取课程信息、视频信息
    @GetMapping("/getCFByCid")
    public Result<List<Map<String,Object>>> getCFByCid(Integer id){
        return Result.success(courseService.listCFByCid(id));
    }
}
